#!/bin/sh

. "$SPEC_PATH/abstract/dbops-major-version-upgrade"

e2e_test_extra_hash() {
  "$SHELL" "$PROJECT_PATH/stackgres-k8s/ci/build/build-functions.sh" path_hash \
    "$(realpath --relative-to "$PROJECT_PATH" "$SPEC_PATH/abstract/dbops-major-version-upgrade")"
}

e2e_test() {
  run_test "Checking that major version upgrade with distributed logs is working" check_major_version_upgrade_with_distributed_logs_is_working
}

check_major_version_upgrade_with_distributed_logs_is_working() {
  DISTRIBUTEDLOGS_NAME="$(get_sgdistributedlogs_name distributedlogs)"

  reset_cluster 4 \
    --set distributedLogs.enabled=true \
    --set distributedLogs.create=true \
    --set-string cluster.distributedLogs.sgDistributedLogs="$DISTRIBUTEDLOGS_NAME" \
    --set-string distributedLogs.persistentVolume.size=128Mi

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGDbOps
metadata:
  name: $DBOPS_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  sgCluster: $CLUSTER_NAME
  op: majorVersionUpgrade
  majorVersionUpgrade:
    postgresVersion: "$E2E_MAJOR_TARGET_POSTGRES_VERSION"
    sgPostgresConfig: postgresconf-for-major-version-upgrade
    postgresExtensions:
    - name: pg_repack
      version: $TARGET_PG_REPACK_VERSION
EOF

  check_major_version_upgrade

  kubectl delete sgdbops -n "$CLUSTER_NAMESPACE" "$DBOPS_NAME"
}
